import sys
import urllib2
import re
import time
import sqlite3
import os
from collections import deque
import ConfigParser


URL = ''
PAGES = 3
DB = 'songs.db'
DEBUG = False
DESTINATION = ''


class hypemMachineSong:
    def __init__(self, id , key, title, artist):
        self.id = id
        self.key = key
        self.title = title.replace('\\','')
        self.artist = artist.replace('\\','')
        self.url = 'http://hypem.com/serve/play/' + id + '/' + key + ".mp3"
        if DEBUG:
            print "__init__ hypemMachineSong : "+ self.url  
            print title + " - " + artist


    def __str__(self):
        return "("+self.key+", "+self.title+", "+self.artist+")"


class HypeScraper:

    def __init__(self):
        if DEBUG:
            print "__init__ HypeScraper"
            print "url : " + 'http://hypem.com/'+URL+'/'
            print "database : " +DB
        self.url = 'http://hypem.com/'+URL+'/'
        self.songs = deque()
        self.conn = sqlite3.connect(DB)
        self.cursor = self.conn.cursor()

    def start(self):
        for i in range(1, PAGES + 1):
            complete_url = self.url + str(i) + '?ax=1&ts='+ str(time.time())
            #complete_url = "http://hypem.com/#!/" + URL + '/' + str(i)
            request = urllib2.Request(complete_url)
            response = urllib2.urlopen(request)
            #save our cookie
            self.current_cookie = response.headers.get('Set-Cookie')
            #grab the HTML
            html_data = response.read()

            if DEBUG:
                html_file = open("hypeHTML.html", "w")
                html_file.write(html_data)
                html_file.close()

            self.parse_html(html_data)

            self.download_songs()


    def download_songs(self):

        while len( self.songs ) > 0:
            current_song = self.songs.popleft()

            print "Attempting to download ", current_song

            if self.song_exists( current_song ):
                print "\tSong existed!"
                continue	

            #download song
            print "\tDownloading song..."
            if (self.download_song(current_song) ):
                print "\tInserted song into db"
                self.insert_song(current_song)



    def download_song(self, song):
        if not os.path.exists(DESTINATION):
            print "Destination Folder doesnt exist"
            sys.exit()
        os.chdir(DESTINATION)
        request = urllib2.Request(song.url)
        request.add_header('cookie', self.current_cookie)
        try:
            response = urllib2.urlopen(request)
        except urllib2.HTTPError  as e:
            print "Error downloading song: ", song
            print(e.code)
            return False
        #grab the data
        song_data = response.read()
        try:
            mp3_song = open(song.title+".mp3", "wb")
            mp3_song.write(song_data)
            mp3_song.close()
        except Exception as e:
            print "Error downloading song: ", song
            return False
        time.sleep(1) #sleep so we aren't booted
        return True

    def insert_song(self, song):
        t = (None, song.id)
        self.cursor.execute("insert into songs values (?, ?)", t)
        self.conn.commit()

    def song_exists(self, song):
        t = (song.id, )
        self.cursor.execute("select * from songs where key=?", t)
        if len(self.cursor.fetchall()) > 0:
            return True
        return False

    def parse_html(self, html_contents):
        idMatches = re.findall("[ \t]id:\'(\w*)(?=\')", html_contents)
        keyMatches = re.findall("(?<=\tkey: \')\w*(?=\')", html_contents)
        songMatches= re.findall("(?<=\tsong:\').*(?=\')", html_contents)
        artistMatches= re.findall("(?<=\tartist:\').*(?=\')", html_contents)

        for i in range( len(idMatches) ):
            id = idMatches[i]
            key = keyMatches[i]
            title = songMatches[i]
            artist = artistMatches[i]		
            song = hypemMachineSong(id, key, title, artist)
            self.songs.append(song)

        if DEBUG:
            print "Found " , len(self.songs) , " songs."



def main():

    scraper = HypeScraper()
    scraper.start()
    scraper.conn.close()




if __name__ == "__main__":
    p = ConfigParser.ConfigParser()
    p.read('config.ini')    
    URL = p.get('Settings','url')
    PAGES = int(p.get('Settings','pages'))
    DB = p.get('Settings','db')
    DESTINATION = p.get('Settings','destination')
    DEBUG = bool(p.get('Settings','DEBUG'))
    main()